/**
 * @param {any} timestamp 时间戳，秒为单位
 * @returns e.g. 1分钟以前
 */

export let timeAgo = function(timestamp) {
    if (typeof timestamp === 'undefined') return '很久以前'
    var dMinutes, dHours, dDays
    var timeNow = parseInt(new Date().getTime() / 1000)
    var d
    d = timeNow - timestamp
    dDays = parseInt(d / 86400)
    dHours = parseInt(d / 3600)
    dMinutes = parseInt(d / 60)
        // console.log("d_days"+d_days+"d_hours"+d_hours+"d_minutes"+d_minutes)
    if (dDays > 0 && dDays < 4) {
        return dDays + '天前'
    } else if (dDays <= 0 && dHours > 0) {
        return dHours + '小时前'
    } else if (dHours <= 0 && dMinutes > 0) {
        return dMinutes + '分钟前'
    } else {
        var s = new Date(timestamp * 1000)
            // s.getFullYear()+"年";
        return s.getFullYear() + '/' + (s.getMonth() + 1) + '/' + s.getDate()
    }
}


/**
 * 获取浏览器cookie,通过cookieName
 * 
 * return cookieValue if exist,otherwise return ""
 * @param {string} cookieName
 * @returns {string} cookieValue 
 */
export let getCookie = function(cookieName) {
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(cookieName + "=")
        if (c_start != -1) {
            var c_start = c_start + cookieName.length + 1
            var c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}